
import _ from "lodash";

import HomeView from "@/views/Home"
import LoginView from "@/views/Login"
import NotFoundView from "@/views/NotFound"

import BannerList from "@/views/Banner/BannerList"
import BannerAdd from "@/views/Banner/BannerAdd"
import ProList from "@/views/Product/ProList"
import MainView from "@/views/Main"

var routes = [
    {
        path: "/",
        name: "home",
        component: HomeView,
        children: [
            { path: "", redirect: "/main" },
            { path: "/main", name: "main", label: "首页", component: MainView },
            {
                path: "/banner/list", name: "bannerList", label: "轮播列表", parent: { name: "banner", label: "轮播图管理" }, component: BannerList, children: [
                    { path: "", redirect: { name: "swiperMode" } },
                    { path: "swiper", name: "swiperMode", component: () => import("@/views/Banner/BannerList/SwiperMode"), meta: { activeName: "swiper" } },
                    { path: "table", name: "tableMode", component: () => import("@/views/Banner/BannerList/TableMode"), meta: { activeName: "table" } }
                ]
            },
            { path: "/banner/add", name: "bannerAdd", label: "轮播新增", parent: { name: "banner", label: "轮播图管理" }, component: BannerAdd },
            { path: "/pro/list", name: "proList", label: "商品列表", parent: { name: "pro", label: "商品管理" }, component: ProList },
            { path: "/pro/search", name: "proSearch", label: "商品搜索", parent: { name: "pro", label: "商品管理" }, component: ()=>import("@/views/Product/ProSearch") },
            { path: "/admin/list", name: "adminList", label: "用户列表", parent: { name: "admin", label: "用户管理" }, component: ()=>import("@/views/Admin/AdminList") }
        ]
    },
    {
        path: "/login",
        name: "login",
        component: LoginView,
    },
    {
        path: "/sass",
        name: "sass",
        component: ()=>import("@/views/Sass"),
    },
    {
        path: "/404",
        name: "notfound",
        component: NotFoundView,
    },
    {
        path: "*",
        redirect: "/404"
    }
]

// 期望根据路由配置信息  -> 生成菜单信息
// [   
//     // 一级导航
//     { path: '/main', name: 'main', label: "首页" },
//     {
//         name: 'banner', label: '轮播图管理', 
//         children: [
//             { path: '/banner/list', name: 'bannerList', label: "轮播列表" },
//             { path: '/banner/list', name: 'bannerList', label: "轮播列表" },
//         ]
//     }
// ]

// 代码实现
// children: [
//     { path: "", redirect: "/main" },  //默认显示的内容   -> 不需要菜单
//     { path: "/main", name: "main", label:"首页", component: MainView },
//     { path: "/banner/list", name: "bannerList", label:"轮播列表", parent:{name:"banner",label:"轮播图管理"} , component: BannerList },
//     { path: "/banner/add", name: "bannerAdd",label:"轮播新增", parent:{name:"banner",label:"轮播图管理"}, component: BannerAdd },
//     { path: "/pro/list", name: "proList", label:"商品列表",  parent:{name:"pro",label:"商品管理"}, component: ProList },
// ]

const transformRoutesToMenu = (routes) => {
    var items = routes[0].children || [];  // 模拟实现 => 默认获取第一个路由 / (主页面下的) 路由配置
    var mapMenu = {
        // main: {path: "/main", name: "main", label:"首页"}   
        // banner: {path:"banner",name:"banner",label:"轮播图管理",children:[{path: "/banner/list", name: "bannerList", label:"轮播列表",}]}

    };
    items.forEach((item) => {
        if (item.path == "") return false;  //默认显示的内容 
        if (!item.parent) {  // 没有parent属性 -> 一级菜单
            mapMenu[item.name] = { path: item.path, name: item.name, label: item.label };  //一级菜单相关的信息
        } else {  // 有parent => 二级菜单
            if (!mapMenu[item.parent.name]) { // 对应名字的一级导航是否生成  => 没有生成
                mapMenu[item.parent.name] = {
                    name: item.parent.name,
                    path: item.parent.name,
                    label: item.parent.label,
                    children: [
                        { path: item.path, name: item.name, label: item.label }
                    ]
                }
            } else {
                mapMenu[item.parent.name].children.push({ path: item.path, name: item.name, label: item.label })
            }
        }
    })

    // return mapMenu;
    return Object.values(mapMenu);

}


export let menus = transformRoutesToMenu(routes);
// console.log(menus);


export const createAuthorityMenu = (menu,authorityInfo)=>{  // authorityInfo = {role, checkedKeys}
    var _menus = _.cloneDeep(menu); 
    var {role, checkedkeys} = authorityInfo;
    // console.log( _.cloneDeep(menu));
    // console.log(role, checkedkeys);  // 允许访问的页面的形参数组
    if(role == 1) { //超级管理员  -> 访问所有页面
        return _menus;   
    }else{ // 不是超级管理员 -> 只能访问允许访问的页面 (checkedKeys)
        // 从menu中找到允许访问 -> 删除不允许访问的  => 权限菜单 (menu对象-> 引用类型 ->影响原数据  -> 需要深拷贝)
        // console.log(_menus); 
        var list = [];
        _menus.forEach((menu)=>{
            if(checkedkeys.includes(menu.name)){  // 直接有name(一级菜单/一级二级都被选中) => 可以访问
                list.push(menu);
            }else if(menu.children){  // 一级可能没有被选中 -> 只选择了二级 
                var subMenus = menu.children;  // 二级菜单
                subMenus.forEach((subMenu,j)=>{
                    if(!checkedkeys.includes(subMenu.name)){   // 二级菜单的名字 -> 在checkedkeys不存在 -> 删掉  -> 剩余的是可以访问的二级
                        subMenus.splice(j,1); 
                    }
                })
                if(subMenus.length>0){   // 只要有一个二级导航被访问 ->  把对应的一级导航放到list中
                    list.push(menu);
                }
            }
        })

        // console.log('list',list);
        return list
    }
}



export default routes;